package com.hh.service.impl;

import com.hh.pojo.Demo;
import com.hh.pojo.PagePojo;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.IntStream;

@Service
public class PageServiceImpl implements PageService {

    @Override
    public PagePojo<Demo> queryPage(PagePojo pagePojo) {
        List<Demo> demoAll = getDemo(pagePojo);
        pagePojo.setPageNo(1);
        List<Demo> demo = demoAll.subList((pagePojo.getPageNo() - 1) * 10, pagePojo.getPageNo() * 10);
        pagePojo.setPageList(demo);
        pagePojo.setTotal(demoAll.size());
        return pagePojo;
    }

    @Override
    public PagePojo<Demo> queryByParamsPage(PagePojo pagePojo, Map<String, Object> params) {
        List<Demo> demoAll = getDemo(pagePojo);

        demoAll=demoAll.stream().filter(demo->{
            return null!=params.get("name")?params.get("name").equals(demo.getName()):true;
        }).toList();

        demoAll=demoAll.stream().filter(demo->{
            return null!=params.get("id")?params.get("id").equals(demo.getId()):true;
        }).toList();

        List<Demo> demo = demoAll.subList((pagePojo.getPageNo() - 1) * 10,demoAll.size()<10?demoAll.size(): pagePojo.getPageNo() * 10);
        pagePojo.setPageList(demo);
        pagePojo.setTotal(demoAll.size());
        return pagePojo;
    }

    private List<Demo> getDemo(PagePojo pagePojo){

        List<Demo> demos = IntStream.range(1, 300).mapToObj(i -> new Demo(i + "",   "达芬奇" + i)).toList();

        return demos;
    }
}
